package pri.zxx.learndemo.threademo.base;

import pri.zxx.webdemo.utils.TimeUtils;

import static java.lang.Thread.currentThread;
import static java.util.stream.IntStream.rangeClosed;

/**
 * @author Twilight
 * @desc
 * @createTime 2020-06-26-11:34
 */
public class TicketWindowDemo {
    public static void main(String[] args) {
        ServerCenter serverCenter = new ServerCenter("办事大厅", 5000);
        rangeClosed(0, 3)
                .mapToObj((s) -> new Thread(serverCenter, "wind-" + s))
                .forEach(Thread::start);
        TimeUtils.pause(1, 0, 0);
    }
}

class ServerCenter implements Runnable {
    private final String windowName;
    private volatile Integer maxNumber;

    public ServerCenter(String windowName, Integer maxNumber) {
        this.windowName = windowName;
        this.maxNumber = maxNumber;
    }

    @Override
    public void run() {
        while (maxNumber > 0) {
            synchronized (windowName) {
                if (maxNumber > 0) {
                    TimeUtils.pause(0, 0, 5);
                    System.out.println(windowName + "--->>" + currentThread().getName() + ":处理" + maxNumber-- + " 票");
                }
            }
        }
    }
}